<?php

namespace app\uapi\controller;

use app\admin\model\KnscyGoods;
use app\admin\model\KnscyOrders;
use app\admin\model\KnscyUsers;
use app\common\controller\ApiController;
use app\common\service\OrderService;
use app\common\service\WeginService;

class Index extends ApiController
{
    /**
     * 设置需要用户token的action方法
     * @var string[]
     */
    public static $needLoginAction = [
        'shareData'
    ];



    public function index()
    {

        $this->success('welcome');
    }

    /**
     * 生成小程序的推广二维码
     * @return void
     */
    public function qrcode()
    {
        $scene = $this->request->get('scene');
        if(empty($scene)){
            $this->error('error');
        }
        $uniacid =   WeginService::getUniacid();
        load()->func('communication');
        /*获取小程序的access_token*/
        load()->classs('wxapp.account');
        try {
            $account = pdo_fetch('select * from ' . tablename('account_wxapp') . " where acid= '" . $uniacid . "'");
            $accObj = new \WxappAccount($account);
            $ACCESS_TOKEN = $accObj->getAccessToken();
        }catch (\Exception $e){
            $this->error('小程序配置有误',$e->getMessage());
        }
        $url = "https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token=$ACCESS_TOKEN";
        //请求小程序二维码生成接口，传递场景 参数
        $body = array('scene' => $scene);
        $ret = ihttp_post($url, json_encode($body));
        header('Content-type: image/jpeg');
        echo $ret['content'];
        exit;
    }

    /**
     * 获取我的分享数据
     * @return void
     */
    public function shareData()
    {
        $user = KnscyUsers::find($this->user_id);

        $data = [
            'nickname' => $user['nickname'] ?? '微信用户',
            'user_id' => $this->user_id,
            'desc' => '为您挑选了一个好物',
            'qrcodeImg' => __url('index/qrcode',['scene' => 'user' . $this->user_id ]),
            'head_img' => $user['head_img'] ? get_full_url($user['head_img']) : tomedia('/addons/kbwdl_nickscenery/public/images/get_avatar.png')
        ];

        $this->success('success', $data);
    }

    /**
     * 获取景区介绍页面数据
     * @return
     */
    public function companyInfo(){

        $data = sysconfig('company');

        if(isset($data['company_logo'])){
            $data['company_logo'] = get_full_url($data['company_logo']);
        }

        if(isset($data['company_content'])){
            $data['company_content'] = str_replace("\n","<br/>", $data['company_content']);
        }

        if(isset($data['company_images'])){
            $arr = explode("|",$data['company_images']);
            foreach($arr as $key=> $log){
                $arr[$key] = get_full_url($log);
            }
            $data['company_images'] =  $arr;
        }


        $this->success('success', $data);

    }
}